---
title: Spec - howl.Buffer
tags: spec
---
<div class="spec-group spec-group-1">

<h1 id="howl.buffer">howl.Buffer</h1>

<pre class="highlight moonscript"><code><span class="n">buffer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">text</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="k">with</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
    </span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">text</span></code></pre>


<h4 id=".size-returns-the-size-of-the-buffer-text,-in-bytes">.size returns the size of the buffer text, in bytes</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">).</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'åäö'</span><span class="p">).</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span></code></pre>


<h4 id=".length-returns-the-size-of-the-buffer-text,-in-characters">.length returns the size of the buffer text, in characters</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">).</span><span class="n">length</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'åäö'</span><span class="p">).</span><span class="n">length</span></code></pre>


<h4 id=".modified-indicates-and-allows-setting-the-modified-status">.modified indicates and allows setting the modified status</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'hello'</span><span class="w"> </span><span class="c1">-- toggling should not have changed text</span></code></pre>


<h4 id=".read_only-can-be-set-to-mark-the-buffer-as-read-only">.read_only can be set to mark the buffer as read-only</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'kept'</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">read_only</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">read_only</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'read%-only'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'illegal'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'read%-only'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'illegal'</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'read%-only'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'illegal'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'kept'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">read_only</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">' yes'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'kept yes'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id=".properties-is-a-table">.properties is a table</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'table'</span><span class="p">,</span><span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">''</span><span class="p">).</span><span class="n">properties</span></code></pre>


<h4 id=".data-is-a-table">.data is a table</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'table'</span><span class="p">,</span><span class="w"> </span><span class="nb">type</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">''</span><span class="p">).</span><span class="n">data</span></code></pre>


<h4 id=".showing-is-true-if-the-buffer-is-currently-referenced-in-any-view">.showing is true if the buffer is currently referenced in any view</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">showing</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">add_view_ref</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">showing</span></code></pre>


<h4 id=".can_undo-returns-true-if-undo-is-possible,-and-false-otherwise">.can_undo returns true if undo is possible, and false otherwise</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'bar'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span></code></pre>


<h4 id="#buffer-returns-the-number-of-characters-in-the-buffer">#buffer returns the number of characters in the buffer</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="o">#</span><span class="n">buffer</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="o">#</span><span class="n">buffer</span><span class="p">(</span><span class="s1">'åäö'</span><span class="p">)</span></code></pre>


<h4 id="tostring(buffer)-returns-the-buffer-title">tostring(buffer) returns the buffer title</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nb">tostring</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="w"> </span><span class="s1">'foo'</span></code></pre>

<div class="spec-group spec-group-2">

<h2 id=".text">.text</h2>

<h4 id=".text-allows-setting-and-retrieving-the-buffer-text">.text allows setting and retrieving the buffer text</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Ipsum'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'Ipsum'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="transforms-incorrect-input">transforms incorrect input</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'|\x80|'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'|�|'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".last_changed">.last_changed</h2>

<pre class="highlight moonscript"><code><span class="n">sys</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">howl</span><span class="p">.</span><span class="n">sys</span></code></pre>


<h4 id="is-set-to-the-current-time-for-a-new-buffer">is set to the current time for a new buffer</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'time'</span><span class="w">
</span><span class="n">now</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">math.floor</span><span class="w"> </span><span class="n">sys</span><span class="p">.</span><span class="n">time</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nb">math.floor</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">now</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nb">math.floor</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="n">now</span></code></pre>


<h4 id="is-updated-whenever-the-buffer-is-changed-in-some-way">is updated whenever the buffer is changed in some way</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'time'</span><span class="w">
</span><span class="n">cur</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="w">

</span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">cur</span><span class="w">

</span><span class="n">cur</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">last_changed</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">cur</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".file-=-&lt;file&gt;">.file = &lt;file&gt;</h2>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span></code></pre>


<h4 id="sets-the-title-to-the-basename-of-the-file">sets the title to the basename of the file</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">basename</span></code></pre>


<h4 id="marks-the-buffer-as-not-modified">marks the buffer as not modified</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>


<h4 id="clears-the-undo-history">clears the undo history</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="when-&lt;file&gt;-exists">when &lt;file&gt; exists</h3>

<h4 id="overwrites-any-existing-buffer-text-even-if-the-buffer-is-modified">overwrites any existing buffer text even if the buffer is modified</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'yes sir'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'yes sir'</span></code></pre>

<div class="spec-group spec-group-4">

<h4 id="and-the-buffer-is-not-modified">and the buffer is not modified</h4>

<pre class="highlight moonscript"><code><span class="n">before_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">reset</span><span class="o">!</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">define</span><span class="w"> </span><span class="ss">name:</span><span class="w"> </span><span class="s1">'buf_var'</span><span class="p">,</span><span class="w"> </span><span class="ss">description:</span><span class="w"> </span><span class="s1">'some var'</span><span class="p">,</span><span class="w"> </span><span class="ss">default:</span><span class="w"> </span><span class="s1">'def value'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'orig_value'</span></code></pre>


<h4 id="sets-the-buffer-text-to-the-contents-of-the-file">sets the buffer text to the contents of the file</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'yes sir'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'yes sir'</span></code></pre>


<h4 id="preserves-the-buffer-config">preserves the buffer config</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="p">,</span><span class="w"> </span><span class="s1">'orig_value'</span></code></pre>


<h4 id="transforms-and-flags-incorrect-utf-8-as-input">transforms and flags incorrect UTF-8 as input</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'|\x80|'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'|�|'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-3">

<h3 id="when-&lt;file&gt;-does-not-exist">when &lt;file&gt; does not exist</h3>

<h4 id="set-the-buffer-text-to-the-empty-string">set the buffer text to the empty string</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id=".eol">.eol</h2>

<h4 id="is-&quot;\\n&quot;-by-default">is &quot;\\n&quot; by default</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'\n'</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">''</span><span class="p">).</span><span class="n">eol</span></code></pre>


<h4 id="raises-an-error-if-a-assignment-value-is-unknown">raises an error if a assignment value is unknown</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'Unknown'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">''</span><span class="p">).</span><span class="n">eol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'foo'</span></code></pre>


<h4 id="is-adjusted-automatically-when-a-file-is-loaded">is adjusted automatically when a file is loaded</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span><span class="w">
</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">plain_file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'o hai\r\nDOS'</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'\r\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">eol</span><span class="w">

    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">plain_file</span><span class="w">
    </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'o hai\nNIX'</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">eol</span><span class="w">

    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">plain_file</span><span class="w">
    </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'venerable\rMac'</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'\r'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">eol</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".multibyte">.multibyte</h2>

<h4 id="returns-true-if-the-buffer-contains-multibyte-characters">returns true if the buffer contains multibyte characters</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'vanilla'</span><span class="p">).</span><span class="n">multibyte</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">buffer</span><span class="p">(</span><span class="s1">'HƏllo'</span><span class="p">).</span><span class="n">multibyte</span></code></pre>


<h4 id="is-updated-whenever-text-is-inserted">is updated whenever text is inserted</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'vanilla'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'Bačon'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">multibyte</span></code></pre>


<h4 id="is-updated-whenever-text-is-deleted">is updated whenever text is deleted</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'Bačon'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">multibyte</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".modified_on_disk">.modified_on_disk</h2>

<h4 id="is-false-for-a-buffer-with-no-file">is false for a buffer with no file</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="nc">Buffer</span><span class="o">!</span><span class="p">.</span><span class="n">modified_on_disk</span></code></pre>


<h4 id="is-true-if-the-file's-etag-is-changed-after-a-load-or-save">is true if the file's etag is changed after a load or save</h4>

<pre class="highlight moonscript"><code><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">contents:</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="ss">etag:</span><span class="w"> </span><span class="s1">'1'</span><span class="p">,</span><span class="w"> </span><span class="ss">basename:</span><span class="w"> </span><span class="s1">'changeable'</span><span class="p">,</span><span class="w"> </span><span class="ss">exists:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="ss">path:</span><span class="w"> </span><span class="s1">'/tmp/changeable'</span><span class="w">
</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="o">!</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
</span><span class="n">file</span><span class="p">.</span><span class="n">etag</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'2'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified_on_disk</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified_on_disk</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".config">.config</h2>

<pre class="highlight moonscript"><code><span class="n">before_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">reset</span><span class="o">!</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">define_layer</span><span class="w"> </span><span class="s1">'mode:config'</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">define</span><span class="w"> </span><span class="ss">name:</span><span class="w"> </span><span class="s1">'buf_var'</span><span class="p">,</span><span class="w"> </span><span class="ss">description:</span><span class="w"> </span><span class="s1">'some var'</span><span class="p">,</span><span class="w"> </span><span class="ss">default:</span><span class="w"> </span><span class="s1">'def value'</span></code></pre>


<h4 id="allows-reading-and-writing-(local)-variables">allows reading and writing (local) variables</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'config'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'def value'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">123</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">123</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'def value'</span><span class="p">,</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span></code></pre>


<h4 id="is-chained-to-the-mode-config-when-available">is chained to the mode config when available</h4>

<pre class="highlight moonscript"><code><span class="n">mode_config</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">proxy</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span><span class="w"> </span><span class="s1">'mode:config'</span><span class="w">
</span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">config_layer:</span><span class="w"> </span><span class="s1">'mode:config'</span><span class="w">
</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'config'</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mode</span><span class="w">
</span><span class="n">mode_config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'from_mode'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'from_mode'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span></code></pre>


<h4 id="is-chained-to-the-global-config-when-mode-config-is-not-available">is chained to the global config when mode config is not available</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'config'</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'def value'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span></code></pre>


<h4 id="each-new-buffer-gets-a-distinct-config">each new buffer gets a distinct config</h4>

<pre class="highlight moonscript"><code><span class="n">b1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'config'</span><span class="w">
</span><span class="n">b2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'config'</span><span class="w">
</span><span class="n">b1</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'b1_value'</span><span class="w">
</span><span class="n">b2</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'b2_value'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'b1_value'</span><span class="p">,</span><span class="w"> </span><span class="n">b1</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'b2_value'</span><span class="p">,</span><span class="w"> </span><span class="n">b2</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span></code></pre>


<h4 id="uses-a-buffer-scoped-config-for-unsaved-files">uses a buffer scoped config for unsaved files</h4>

<pre class="highlight moonscript"><code><span class="n">b1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'unsaved'</span><span class="w">
</span><span class="n">b1</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'b1_value'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'b1_value'</span><span class="p">,</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="s1">'buf_var'</span><span class="p">,</span><span class="w"> </span><span class="s1">'buffer/'</span><span class="o">..</span><span class="n">b1</span><span class="p">.</span><span class="n">id</span></code></pre>


<h4 id="uses-a-file-scoped-config-when-associated-with-a-file">uses a file scoped config when associated with a file</h4>

<pre class="highlight moonscript"><code><span class="n">b1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'saved'</span><span class="w">
</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b1</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b1</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">buf_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'f1_value'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'f1_value'</span><span class="p">,</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="s1">'buf_var'</span><span class="p">,</span><span class="w"> </span><span class="s1">'file'</span><span class="o">..</span><span class="n">file</span><span class="p">.</span><span class="n">path</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'def value'</span><span class="p">,</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="s1">'buf_var'</span><span class="p">,</span><span class="w"> </span><span class="s1">'buffer'</span><span class="o">..</span><span class="n">b1</span><span class="p">.</span><span class="n">id</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="delete(start_pos,-end_pos)">delete(start_pos, end_pos)</h2>

<h4 id="deletes-the-specified-range,-inclusive">deletes the specified range, inclusive</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'ño örf'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ñrf'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="does-nothing-if-end_pos-is-smaller-than-start_pos">does nothing if end_pos is smaller than start_pos</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hello'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="insert(text,-pos)">insert(text, pos)</h2>

<h4 id="inserts-text-at-pos">inserts text at pos</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'ño señor'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'me gusta '</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ño me gusta señor'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="returns-the-position-right-after-the-inserted-text">returns the position right after the inserted text</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'Bačon'</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span></code></pre>


<h4 id="transforms-incorrect-input">transforms incorrect input</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'|\x80|'</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'|�|'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="append(text)">append(text)</h2>

<h4 id="appends-the-specified-text">appends the specified text</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">' world'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hello world'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="returns-the-position-right-after-the-inserted-text">returns the position right after the inserted text</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'Bačon'</span></code></pre>


<h4 id="transforms-incorrect-input">transforms incorrect input</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'|\x80|'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'|�|'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="replace(pattern,-replacement)">replace(pattern, replacement)</h2>

<h4 id="replaces-all-occurences-of-pattern-with-replacement">replaces all occurences of pattern with replacement</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello\nuñi©ode\nworld\n'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">replace</span><span class="w"> </span><span class="s1">'[lo]'</span><span class="p">,</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'he\nuñi©de\nwrd\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="transforms-incorrect-replacements">transforms incorrect replacements</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'XX'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">replace</span><span class="w"> </span><span class="s1">'X'</span><span class="p">,</span><span class="w"> </span><span class="s1">'\x80'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'��'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="returns-the-number-of-occurences-replaced">returns the number of occurences replaced</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello\nworld\n'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">replace</span><span class="p">(</span><span class="s1">'world'</span><span class="p">,</span><span class="w"> </span><span class="s1">'editor'</span><span class="p">)</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(when-pattern-contains-a-leading-grouping)">(when pattern contains a leading grouping)</h3>

<h4 id="replaces-only-the-match-within-pattern-with-replacement">replaces only the match within pattern with replacement</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello\nworld\n'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">replace</span><span class="w"> </span><span class="s1">'(hel)lo'</span><span class="p">,</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'lo\nworld\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="change(start_pos,-end_pos,-f)">change(start_pos, end_pos, f)</h2>

<h4 id="applies-all-operations-as-one-undo-for-the-specified-region">applies all operations as one undo for the specified region</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'ño señor'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">change</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="c1">-- 'señ'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">insert</span><span class="w"> </span><span class="s1">'minmin'</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w">

</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ño minminor'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ño señor'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="returns-the-return-value-of-&lt;f&gt;-as-its-own-return-value">returns the return value of &lt;f&gt; as its own return value</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'12345'</span><span class="w">
</span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">change</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="s1">'zed'</span><span class="w">

</span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'zed'</span><span class="p">,</span><span class="w"> </span><span class="n">ret</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="undo">undo</h2>

<h4 id="undoes-the-last-operation">undoes the last operation</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hello'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="resets-the-.modified-flag-when-at-last-saved-file-revision">resets the .modified flag when at last saved file revision</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>


<h4 id="resets-the-.modified-flag-when-at-originally-loaded-revision">resets the .modified flag when at originally loaded revision</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello hello'</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="redo">redo</h2>

<h4 id="redoes-the-last-undo-operation">redoes the last undo operation</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">redo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ello'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="resets-the-.modified-flag-when-at-synced-file-revision">resets the .modified flag when at synced file revision</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">redo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">redo</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".can_undo-=-&lt;bool&gt;">.can_undo = &lt;bool&gt;</h2>

<h4 id="setting-it-to-false-removes-any-undo-history">setting it to false removes any undo history</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'hello'</span></code></pre>


<h4 id="setting-it-to-true-is-a-no-op">setting it to true is a no-op</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".collect_revisions">.collect_revisions</h2>
<div class="spec-group spec-group-3">

<h3 id="(when-set-to-false)">(when set to false)</h3>

<h4 id="does-not-collect-undo-information">does not collect undo information</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">collect_revisions</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'foo!'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span></code></pre>


<h4 id="clears-existing-undo-information">clears existing undo information</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'zed'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">collect_revisions</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">can_undo</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="as_one_undo(f)">as_one_undo(f)</h2>

<h4 id="allows-for-grouping-actions-as-one-undo">allows for grouping actions as one undo</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">as_one_undo</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hello'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(when-f-raises-an-error)">(when f raises an error)</h3>

<h4 id="propagates-the-error">propagates the error</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'oh my'</span><span class="p">,</span><span class="w">  </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">as_one_undo</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nb">error</span><span class="w"> </span><span class="s1">'oh my'</span></code></pre>


<h4 id="ends-the-undo-transaction">ends the undo transaction</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">as_one_undo</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="nb">error</span><span class="w"> </span><span class="s1">'oh noes what happened?!?'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">undo</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s1">'ello'</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="save()">save()</h2>
<div class="spec-group spec-group-3">

<h3 id="(when-a-file-is-assigned)">(when a file is assigned)</h3>

<h4 id="stores-the-contents-of-the-buffer-in-the-assigned-file">stores the contents of the buffer in the assigned file</h4>

<pre class="highlight moonscript"><code><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'line1\nline2♥\nåäö\n'</span><span class="w">
</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="n">text</span><span class="w">
</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">text</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span></code></pre>


<h4 id="clears-the-modified-flag">clears the modified flag</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">' bar'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">modified</span></code></pre>

<div class="spec-group spec-group-4">

<h3 id="(..--when-config.strip_trailing_whitespace-is-false)">(..  when config.strip_trailing_whitespace is false)</h3>

<h4 id="does-not-strip-trailing-whitespace-before-saving">does not strip trailing whitespace before saving</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">strip_trailing_whitespace</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'blank  \n\nfoo \n'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'blank  \n\nfoo \n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-4">

<h3 id="(..--when-config.strip_trailing_whitespace-is-true)">(..  when config.strip_trailing_whitespace is true)</h3>

<h4 id="strips-trailing-whitespace-at-the-end-of-lines-before-saving">strips trailing whitespace at the end of lines before saving</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">strip_trailing_whitespace</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'åäö  \n\nfoo  \n  '</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'åäö\n\nfoo\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-4">

<h3 id="(..--when-config.ensure_newline_at_eof-is-true)">(..  when config.ensure_newline_at_eof is true)</h3>

<h4 id="appends-a-newline-if-necessary">appends a newline if necessary</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">ensure_newline_at_eof</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'look mah no newline!'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'look mah no newline!\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-4">

<h3 id="(..--when-config.ensure_newline_at_eof-is-false)">(..  when config.ensure_newline_at_eof is false)</h3>

<h4 id="does-not-appends-a-newline">does not appends a newline</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">config</span><span class="p">.</span><span class="n">ensure_newline_at_eof</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'look mah no newline!'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'look mah no newline!'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
<div class="spec-group spec-group-4">

<h3 id="(..--when-config.backup_files-is-true)">(..  when config.backup_files is true)</h3>

<h4 id="backs-up-files-before-saving">backs up files before saving</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">mockfile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{}</span><span class="w">
  </span><span class="nb">setmetatable</span><span class="w"> </span><span class="n">mockfile</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="ss">__index:</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="ss">__newindex:</span><span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
      </span><span class="nb">assert</span><span class="w"> </span><span class="n">key</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s1">'contents'</span><span class="p">,</span><span class="w"> </span><span class="s1">'mock to test backup_files'</span><span class="w">
      </span><span class="k">return</span><span class="w"> </span><span class="n">file</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="w">
  </span><span class="p">}</span><span class="w">

  </span><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">backups</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">config</span><span class="p">.</span><span class="n">backup_files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w">
    </span><span class="n">config</span><span class="p">.</span><span class="n">backup_directory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">backups</span><span class="p">.</span><span class="n">path</span><span class="w">

    </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">

    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mockfile</span><span class="w">
    </span><span class="nb">pcall</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">

    </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="o">#</span><span class="n">backups</span><span class="p">.</span><span class="n">children</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">not_nil</span><span class="w"> </span><span class="n">backups</span><span class="p">.</span><span class="n">children</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">basename</span><span class="o">\</span><span class="n">match</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">basename</span></code></pre>

</div>
</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="save_as(file)">save_as(file)</h2>

<h4 id="associates-the-buffer-with-&lt;file&gt;-henceforth">associates the buffer with &lt;file&gt; henceforth</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'orig'</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">new_file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'nuevo'</span><span class="w">
    </span><span class="n">b</span><span class="o">\</span><span class="n">save_as</span><span class="w"> </span><span class="n">new_file</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'nuevo'</span><span class="p">,</span><span class="w"> </span><span class="n">new_file</span><span class="p">.</span><span class="n">contents</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">new_file</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">file</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(when-&lt;file&gt;-does-not-exist)">(when &lt;file&gt; does not exist)</h3>

<h4 id="saves-the-buffer-content-in-the-newly-created-file">saves the buffer content in the newly created file</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'new'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save_as</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'new'</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span></code></pre>

</div>
<div class="spec-group spec-group-3">

<h3 id="(when-&lt;file&gt;-exists)">(when &lt;file&gt; exists)</h3>

<h4 id="overwrites-any-previous-content-with-the-buffer-contents">overwrites any previous content with the buffer contents</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'old'</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'new'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">save_as</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'new'</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="byte_offset(char_offset)">byte_offset(char_offset)</h2>

<h4 id="returns-the-byte-offset-for-the-given-&lt;char_offset&gt;">returns the byte offset for the given &lt;char_offset&gt;</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'äåö'</span><span class="w">
</span><span class="k">for</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="p">{</span><span class="w">
  </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">},</span><span class="w">
</span><span class="p">}</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">byte_offset</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span></code></pre>


<h4 id="adjusts-out-of-bounds-offsets">adjusts out-of-bounds offsets</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w">  </span><span class="n">buffer</span><span class="s1">'äåö'</span><span class="o">\</span><span class="n">byte_offset</span><span class="w"> </span><span class="mi">5</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w">  </span><span class="n">buffer</span><span class="s1">'äåö'</span><span class="o">\</span><span class="n">byte_offset</span><span class="w"> </span><span class="mi">10</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">  </span><span class="n">buffer</span><span class="s1">'äåö'</span><span class="o">\</span><span class="n">byte_offset</span><span class="w"> </span><span class="mi">0</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">  </span><span class="n">buffer</span><span class="s1">'äåö'</span><span class="o">\</span><span class="n">byte_offset</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="char_offset(byte_offset)">char_offset(byte_offset)</h2>

<h4 id="returns-the-character-offset-for-the-given-&lt;byte_offset&gt;">returns the character offset for the given &lt;byte_offset&gt;</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'äåö'</span><span class="w">
</span><span class="k">for</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="p">{</span><span class="w">
  </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">},</span><span class="w">
  </span><span class="p">{</span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">},</span><span class="w">
</span><span class="p">}</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">char_offset</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span></code></pre>


<h4 id="adjusts-out-of-bounds-offsets">adjusts out-of-bounds offsets</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="s1">'ab'</span><span class="o">\</span><span class="n">char_offset</span><span class="w"> </span><span class="mi">4</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="s1">'äåö'</span><span class="o">\</span><span class="n">char_offset</span><span class="w"> </span><span class="mi">0</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="s1">'a'</span><span class="o">\</span><span class="n">char_offset</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="sub(start_pos,-end_pos)">sub(start_pos, end_pos)</h2>

<h4 id="returns-the-text-between-start_pos-and-end_pos,-both-inclusive">returns the text between start_pos and end_pos, both inclusive</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hållö\nhållö\n'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'h'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'å'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hållö'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hållö\nhållö\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ållö'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">11</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="mi">13</span><span class="p">)</span></code></pre>


<h4 id="handles-negative-indices-by-counting-from-end">handles negative indices by counting from end</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'hållö\nhållö\n'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hållö\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hållö\nhållö\n'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="o">-</span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="p">)</span></code></pre>


<h4 id="returns-empty-string-for-start_pos-&gt;-end_pos">returns empty string for start_pos &gt; end_pos</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'abc'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">sub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span></code></pre>


<h4 id="handles-out-of-bounds-offsets-gracefully">handles out-of-bounds offsets gracefully</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="s1">'abc'</span><span class="o">\</span><span class="n">sub</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'abc'</span><span class="p">,</span><span class="w"> </span><span class="n">buffer</span><span class="s1">'abc'</span><span class="o">\</span><span class="n">sub</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="find(pattern-[,-init-])">find(pattern [, init ])</h2>

<h4 id="searches-forward">searches forward</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'ä öx'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'ä öx'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">' ö'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'öx'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'x'</span><span class="w"> </span><span class="p">}</span></code></pre>


<h4 id="searches-forward-from-init-when-specified">searches forward from init when specified</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'öåååö'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="p">(</span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span></code></pre>


<h4 id="negative-init-specifies-offset-from-end">negative init specifies offset from end</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'öååååö'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">3</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="p">(</span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">2</span><span class="p">)</span></code></pre>


<h4 id="returns-nil-for-out-of-bounds-init">returns nil for out of bounds init</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'abcde'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">6</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">find</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="rfind(pattern-[,-init-])">rfind(pattern [, init ])</h2>

<h4 id="searches-backward-from-end">searches backward from end</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'äöxöx'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'äöx'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'öx'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'x'</span><span class="w"> </span><span class="p">}</span></code></pre>


<h4 id="searches-backward-from-init-when-specified">searches backward from init when specified</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'öååååö'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="p">(</span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span></code></pre>


<h4 id="negative-init-specifies-offset-from-end">negative init specifies offset from end</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'öååååö'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">2</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="w"> </span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">4</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="p">(</span><span class="s1">'åå'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="p">)</span></code></pre>


<h4 id="returns-nil-for-out-of-bounds-init">returns nil for out of bounds init</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'abcde'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">6</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">rfind</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="mode_at(pos)">mode_at(pos)</h2>

<h4 id="returns-the-mode-at-the-given-offset">returns the mode at the given offset</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'abc def ghi jkl'</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">comment_syntax:</span><span class="w"> </span><span class="s1">'//'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">mode_at_test</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">comment_syntax:</span><span class="w"> </span><span class="s1">'#'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">mode_reg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">name:</span><span class="w"> </span><span class="s1">'mode_at_test'</span><span class="p">,</span><span class="w"> </span><span class="ss">create:</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">mode_at_test</span><span class="w">
</span><span class="n">howl</span><span class="p">.</span><span class="n">mode</span><span class="p">.</span><span class="n">register</span><span class="w"> </span><span class="n">mode_reg</span><span class="w">

</span><span class="n">b</span><span class="p">.</span><span class="n">_buffer</span><span class="o">\</span><span class="n">style</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'s1'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w">
  </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'s3'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="s1">'mode_at_test|s2'</span><span class="p">,</span><span class="w">
  </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'s3'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="s1">'nonexistent_mode|s2'</span><span class="p">,</span><span class="w">
  </span><span class="mi">13</span><span class="p">,</span><span class="w"> </span><span class="s1">'s1'</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">3</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">4</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'#'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">5</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'#'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">6</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'#'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">7</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">8</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">9</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">10</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">11</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">12</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">13</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">14</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="s1">'//'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">mode_at</span><span class="p">(</span><span class="mi">15</span><span class="p">).</span><span class="n">comment_syntax</span><span class="w">

</span><span class="n">howl</span><span class="p">.</span><span class="n">mode</span><span class="p">.</span><span class="n">unregister</span><span class="w"> </span><span class="s1">'mode_at_test'</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="reload(force-=-false)">reload(force = false)</h2>

<h4 id="reloads-the-buffer-contents-from-file-and-returns-true">reloads the buffer contents from file and returns true</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'there'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">reload</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'there'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="raises-an-error-if-the-buffer-is-not-associated-with-a-file">raises an error if the buffer is not associated with a file</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'file'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nc">Buffer</span><span class="o">!\</span><span class="n">reload</span><span class="o">!</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(when-the-buffer-is-modified)">(when the buffer is modified)</h3>

<h4 id="leaves-the-buffer-alone-and-returns-false">leaves the buffer alone and returns false</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">' world'</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'there'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">reload</span><span class="o">!</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'hello world'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>


<h4 id="specifying-&lt;force&gt;-as-true-reloads-the-buffer-anyway">specifying &lt;force&gt; as true reloads the buffer anyway</h4>

<pre class="highlight moonscript"><code><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">' world'</span><span class="w">
  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'there'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">reload</span><span class="w"> </span><span class="kc">true</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'there'</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">text</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id=".add_view_ref()">.add_view_ref()</h2>

<h4 id="increments-the-number-of-viewers">increments the number of viewers</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">add_view_ref</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">viewers</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id=".remove_view_ref()">.remove_view_ref()</h2>

<h4 id="decrements-the-number-of-viewers">decrements the number of viewers</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">add_view_ref</span><span class="o">!</span><span class="w">
</span><span class="n">b</span><span class="o">\</span><span class="n">remove_view_ref</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">viewers</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="resolve_span(span,-line_nr-=-nil)">resolve_span(span, line_nr = nil)</h2>

<pre class="highlight moonscript"><code><span class="kd">local</span><span class="w"> </span><span class="n">buf</span><span class="w">

</span><span class="n">before_each</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'123åäö789'</span></code></pre>


<h4 id="accepts-.start_pos-and-.end_pos">accepts .start_pos and .end_pos</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">start_pos:</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="ss">end_pos:</span><span class="w"> </span><span class="mi">7</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_start_pos-as-the-start-specifier">accepts .byte_start_pos as the start specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">byte_start_pos:</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="ss">end_pos:</span><span class="w"> </span><span class="mi">7</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_end_pos-as-the-end-specifier">accepts .byte_end_pos as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">start_pos:</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="ss">byte_end_pos:</span><span class="w"> </span><span class="mi">6</span><span class="p">}</span></code></pre>


<h4 id="accepts-.count-as-the-end-specifier">accepts .count as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">start_pos:</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="ss">count:</span><span class="w"> </span><span class="mi">3</span><span class="p">}</span></code></pre>


<h4 id="accepts-a-sole-line_nr-argument,-returning-the-entire-line's-span">accepts a sole line_nr argument, returning the entire line's span</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">line_nr:</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span></code></pre>

<div class="spec-group spec-group-3">

<h3 id="(with-a-.line_nr-given)">(with a .line_nr given)</h3>

<h4 id="accepts-.start_column-as-the-start-specifier">accepts .start_column as the start specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">line_nr:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">start_column:</span><span class="w"> </span><span class="mi">5</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_start_column-as-the-start-specifier">accepts .byte_start_column as the start specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">line_nr:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">byte_start_column:</span><span class="w"> </span><span class="mi">6</span><span class="p">}</span></code></pre>


<h4 id="accepts-.end_column-as-the-end-specifier">accepts .end_column as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">line_nr:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">end_column:</span><span class="w"> </span><span class="mi">5</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_end_column-as-the-end-specifier">accepts .byte_end_column as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="ss">line_nr:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">byte_end_column:</span><span class="w"> </span><span class="mi">6</span><span class="p">}</span></code></pre>

</div>
<div class="spec-group spec-group-3">

<h3 id="(with-a-line_nr-parameter-given)">(with a line_nr parameter given)</h3>

<h4 id="accepts-.start_column-as-the-start-specifier">accepts .start_column as the start specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="p">{</span><span class="ss">start_column:</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_start_column-as-the-start-specifier">accepts .byte_start_column as the start specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="p">{</span><span class="ss">byte_start_column:</span><span class="w"> </span><span class="mi">6</span><span class="p">},</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span></code></pre>


<h4 id="accepts-.end_column-as-the-end-specifier">accepts .end_column as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="p">{</span><span class="ss">end_column:</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span></code></pre>


<h4 id="accepts-.byte_end_column-as-the-end-specifier">accepts .byte_end_column as the end specifier</h4>

<pre class="highlight moonscript"><code><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">buf</span><span class="o">\</span><span class="n">resolve_span</span><span class="w"> </span><span class="p">{</span><span class="ss">byte_end_column:</span><span class="w"> </span><span class="mi">6</span><span class="p">},</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span></code></pre>

</div>
</div>
<div class="spec-group spec-group-2">

<h2 id="get_ptr(start_pos,-end_pos)">get_ptr(start_pos, end_pos)</h2>

<pre class="highlight moonscript"><code><span class="n">assert_returns</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="n">count</span><span class="p">,</span><span class="w"> </span><span class="o">...</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">r_ptr</span><span class="p">,</span><span class="w"> </span><span class="n">r_count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">...</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="n">count</span><span class="p">,</span><span class="w"> </span><span class="n">r_count</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="n">ffi</span><span class="p">.</span><span class="n">string</span><span class="p">(</span><span class="n">r_ptr</span><span class="p">,</span><span class="w"> </span><span class="n">r_count</span><span class="p">)</span></code></pre>


<h4 id="returns-a-pointer-to-a-char-buffer-for-the-span,-and-a-byte-count">returns a pointer to a char buffer for the span, and a byte count</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'123456789'</span><span class="w">
</span><span class="n">assert_returns</span><span class="w"> </span><span class="s1">'3456'</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span><span class="w">

</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'1åäö8'</span><span class="w">
</span><span class="n">assert_returns</span><span class="w"> </span><span class="s1">'äö'</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span></code></pre>


<h4 id="handles-boundary-conditions">handles boundary conditions</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'123'</span><span class="w">
</span><span class="n">assert_returns</span><span class="w"> </span><span class="s1">'123'</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w">
</span><span class="n">assert_returns</span><span class="w"> </span><span class="s1">'1'</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span></code></pre>


<h4 id="returns-a-zero-pointer-for-an-empty-range">returns a zero pointer for an empty range</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">''</span><span class="w">
</span><span class="n">assert_returns</span><span class="w"> </span><span class="s1">''</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span></code></pre>


<h4 id="raises-errors-for-illegal-span-values">raises errors for illegal span values</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'123'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'Illegal'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'Illegal'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'Illegal'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span></code></pre>


<h4 id="returns-a-read-only-pointer">returns a read-only pointer</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'123'</span><span class="w">
</span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="o">\</span><span class="n">get_ptr</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'constant'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">ptr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">88</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="titles">titles</h2>

<h4 id="uses-file-basename-as-the-default-title">uses file basename as the default title</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/path/to/file.ext'</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="s1">'file.ext'</span></code></pre>


<h4 id="setting-title-explicitly-overrides-default-title">setting title explicitly overrides default title</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Buffer</span><span class="w"> </span><span class="p">{}</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/path/to/file.ext'</span><span class="p">)</span><span class="w">
</span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'be something else'</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="s1">'be something else'</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h2 id="signals">signals</h2>

<h4 id="buffer-saved-is-fired-whenever-a-buffer-is-saved">buffer-saved is fired whenever a buffer is saved</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'buffer-saved'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">b</span><span class="o">\</span><span class="n">save</span><span class="o">!</span><span class="w">

  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="text-inserted-is-fired-whenever-text-is-inserted-into-a-buffer">text-inserted is fired whenever text is inserted into a buffer</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'text-inserted'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'bar'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="text-deleted-is-fired-whenever-text-is-deleted-from-buffer">text-deleted is fired whenever text is deleted from buffer</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'text-inserted'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="text-changed-is-fired-whenever-text-is-change:d-from-buffer">text-changed is fired whenever text is change:d from buffer</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'text-changed'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">change</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">b</span><span class="o">\</span><span class="n">delete</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="buffer-modified-is-fired-whenever-a-buffer-is-modified">buffer-modified is fired whenever a buffer is modified</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'buffer-modified'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="o">\</span><span class="n">append</span><span class="w"> </span><span class="s1">'bar'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="buffer-reloaded-is-fired-whenever-a-buffer-is-reloaded">buffer-reloaded is fired whenever a buffer is reloaded</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'buffer-reloaded'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
    </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
    </span><span class="n">b</span><span class="p">.</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">
    </span><span class="n">b</span><span class="o">\</span><span class="n">reload</span><span class="o">!</span><span class="w">
    </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="buffer-mode-set-is-fired-whenever-a-buffer-has-its-mode-set">buffer-mode-set is fired whenever a buffer has its mode set</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'buffer-mode-set'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{}</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>


<h4 id="buffer-title-set-is-fired-whenever-a-buffer-has-its-title-set">buffer-title-set is fired whenever a buffer has its title set</h4>

<pre class="highlight moonscript"><code><span class="n">with_signal_handler</span><span class="w"> </span><span class="s1">'buffer-title-set'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">handler</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">
  </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foo'</span><span class="w">
  </span><span class="n">b</span><span class="p">.</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Sir Buffer'</span><span class="w">
  </span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">handler</span><span class="p">).</span><span class="n">was_called</span><span class="o">!</span></code></pre>

</div>
<div class="spec-group spec-group-2">

<h3 id="(resource-management)">(resource management)</h3>

<h4 id="buffers-are-collected-properly">buffers are collected properly</h4>

<pre class="highlight moonscript"><code><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer</span><span class="w"> </span><span class="s1">'foobar'</span><span class="w">
</span><span class="n">buffers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">setmetatable</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="ss">__mode:</span><span class="w"> </span><span class="s1">'v'</span><span class="w">
</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">nil</span><span class="w">
</span><span class="nb">collectgarbage</span><span class="o">!</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">is_nil</span><span class="w"> </span><span class="n">buffers</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span></code></pre>

</div>
</div>
